package com.aforce.entities;

import java.awt.geom.Point2D;
import java.io.File;


import java.net.MalformedURLException;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Group;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Light;
import javax.media.j3d.OrientedShape3D;
import javax.media.j3d.PointLight;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Raster;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.aforce.managers.MapManager;
import com.aforce.managers.TimeManager;
import com.sun.j3d.utils.image.TextureLoader;

public class Bullet extends AbstractFlyingEntity
{
	public enum BulletColor
	{
		RED, BLUE, GREEN
	};
	
	final boolean spin = false;
	ImageComponent2D image;
	TransformGroup group = new TransformGroup();
	long timeLastUpdated = TimeManager.getTime();
	
	public Bullet(BulletColor color, Point3d position, Vector3d forward)
	{
		super(position, forward);
		
		try
		{
			switch(color)
			{
				case RED: image = new TextureLoader(new File("./data/redbullet.png").toURI().toURL(), null).getImage(); break;
				case GREEN: image = new TextureLoader(new File("./data/greenbullet.png").toURI().toURL(), null).getImage(); break;
				case BLUE: image = new TextureLoader(new File("./data/bluebullet.png").toURI().toURL(), null).getImage(); break;
				default: throw new RuntimeException("Unknown Bullet Color");
			}
			
			
	        
			Raster raster = new Raster( );
			raster.setPosition( new Point3f( 0.0f, 0.0f, 0.0f ) );
			raster.setType( Raster.RASTER_COLOR );
			raster.setSize( 250, 250);
			raster.setImage( image );
			raster.setClipMode(Raster.CLIP_IMAGE);
			
			TransparencyAttributes ta =  new TransparencyAttributes( TransparencyAttributes.BLENDED, 0.0f );
			Appearance app = new Appearance();
			app.setTransparencyAttributes( ta );

			OrientedShape3D os3d = new OrientedShape3D(raster, app, OrientedShape3D.ROTATE_ABOUT_POINT, new Point3f( 0.0f, 0.0f, 0.0f ));
			os3d.setConstantScaleEnable(!os3d.getConstantScaleEnable());
			group.addChild(os3d);
			
			
			group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
			group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
			
			
			
			
			
			Color3f color3f = new Color3f(0.0f, 1.0f, 0.0f);

			// Create a bounds for the background and lights
			BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

			Light lgt1 = null;

			Point3f lPoint  = new Point3f(0.0f, 0.0f, 0.0f);
			Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);

			lgt1 = new PointLight(color3f, lPoint, atten);
			lgt1.setInfluencingBounds(bounds);
			group.addChild(lgt1);
			
			Transform3D t3d = new Transform3D();
			t3d.setTranslation(new Vector3f(0.7f, 0.7f, 0.7f));
			group.setTransform(t3d);
		}
		catch (MalformedURLException e) { throw new RuntimeException(); }
		
	}
	
	public void update()
	{	
		long milliseconds = TimeManager.getTime() - timeLastUpdated;
		Vector3d forward = new Vector3d(getForwardVector());
		forward.scale(milliseconds * 0.00001);
		getPosition().add(forward);
		
		Transform3D transform = new Transform3D();
		transform.setTranslation(new Vector3d(getPosition()));
		getTransformGroup().setTransform(transform);
	}
	

	@Override
	public TransformGroup getTransformGroup()
	{
		return group;
	}
	
}
